package com.qianli.user.facade.query.impl;

import com.fqgj.common.api.Response;
import com.qianli.user.application.UserAccessApplication;
import com.qianli.user.application.UserAuthApplication;
import com.qianli.user.application.UserFrozenApplication;
import com.qianli.user.domain.model.UserAccessModel;
import com.qianli.user.facade.query.UserQueryServiceFacade;
import com.qianli.user.ro.Paged;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.account.UserAccessRO;
import com.qianli.user.ro.account.UserRO;
import com.qianli.user.ro.auth.CustomerRO;
import com.qianli.user.ro.frozen.UserFrozenRO;
import com.qianli.user.ro.query.UserConditionQueryRO;
import com.qianli.user.ro.query.UserQueryRO;
import com.qianli.user.service.UserQueryService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * name:用户查询
 * auth:ting.li@qianli-inc.com
 * date:2018/1/8 21:12
 * version: v1.0.0
 **/
@Service("userQueryServiceFacade")
public class UserQueryServiceFacadeImpl implements UserQueryServiceFacade{

    private static final Logger LOGGER = LoggerFactory.getLogger(UserQueryServiceFacade.class);

    @Autowired
    private UserAccessApplication userAccessApplication;
    @Autowired
    private UserAuthApplication userAuthApplication;

    @Autowired
    private UserFrozenApplication userFrozenApplication;
    @Autowired
    private UserQueryService userQueryService;
    /**
     * 根据usrecode查询用户信息
     * @param query
     * @return
     */
    @Override
    public Response<UserRO> getUserByUserCode(UserQueryRO query) {
        if(null == query){
            LOGGER.info("UserQueryServiceFacade.getUserByUserCode param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getAppCode()){
            LOGGER.info("UserQueryServiceFacade.getUserByUserCode param appCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getBiz()){
            LOGGER.info("UserQueryServiceFacade.getUserByUserCode param biz can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getUserCode()){
            LOGGER.info("UserQueryServiceFacade.getUserByUserCode param userCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        return getUserRO(query);
    }
    /**
     * 根据手机号查询用户信息
     * @param query
     * @return
     */
    @Override
    public Response<UserRO> getUserByMobile(UserQueryRO query) {
        if(null == query){
            LOGGER.info("UserQueryServiceFacade.getUserByMobile param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getAppCode()){
            LOGGER.info("UserQueryServiceFacade.getUserByMobile param appCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getBiz()){
            LOGGER.info("UserQueryServiceFacade.getUserByMobile param biz can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getMobile()){
            LOGGER.info("UserQueryServiceFacade.getUserByMobile param userCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }

        return getUserRO(query);
    }

    /**
     * 身份证查询用户信息
     * @param query
     * @return
     */
    @Override
    public Response<UserRO> getUserByIdentityNo(UserQueryRO query) {
        if(null == query){
            LOGGER.info("UserQueryServiceFacade.getUserByIdentityNo param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getAppCode()||null == query.getBiz()){
            LOGGER.info("UserQueryServiceFacade.getUserByIdentityNo param appCode and biz can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"appCode and biz can not be null");
        }
        if(null == query.getIdentityNo()){
            LOGGER.info("UserQueryServiceFacade.getUserByIdentityNo param identityNo can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"identityNo can not be null");
        }
        Response<UserRO> response = userQueryService.getUserByIdentityNo(query);
        return response;
    }

    @Override public Response<UserRO> getUserByName(UserQueryRO query) {
        return null;
    }

    /**
     * 按注册来源分页查询注册信息
     * @param query
     * @return
     */
    @Override
    public Response<Paged<List<UserAccessRO>>> pagedQueryUserByRegisterFrom(
            UserConditionQueryRO query) {
        if(null == query){
            LOGGER.info("UserQueryServiceFacade.pagedQueryUserByRegisterFrom param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getAppCode()||null == query.getBiz()){
            LOGGER.info("UserQueryServiceFacade.pagedQueryUserByRegisterFrom param appCode and biz can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"appCode and biz can not be null");
        }
        if(null == query.getRegisterFrom()){
            LOGGER.info("UserQueryServiceFacade.pagedQueryUserByRegisterFrom param registerFrom can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"registerFrom can not be null");
        }
        if(query.getPageSize()>500){
            LOGGER.info("UserQueryServiceFacade.pagedQueryUserByRegisterFrom param pageSize cannot over 500,real:"+query.getPageSize());
            return Response.error(UserStateCode.ILLEGAL_PARAMS,"param pageSize cannot over 500");
        }
        Paged<List<UserAccessRO>> paged =userQueryService.pagedQueryUserByRegisterFrom(query);
        return Response.ok(paged);
    }

    /**
     * 查询用户信息
     * @param query
     * @return
     */
    private Response<UserRO> getUserRO(UserQueryRO query){
        //用户信息
        UserAccessModel userAccessModel = new UserAccessModel();
        userAccessModel.setAppCode(query.getAppCode());
        userAccessModel.setBiz(query.getBiz());
        userAccessModel.setUserCode(query.getUserCode());
        userAccessModel.setMobile(query.getMobile());
        Response<UserAccessRO> accessModelResponse = userAccessApplication.queryUserAccess(userAccessModel);
        if(!accessModelResponse.isSuccess()){
            return Response.error(UserStateCode.USER_NOT_EXITS);
        }
        UserAccessRO userAccessRO = accessModelResponse.getData();
        UserRO userRO = new UserRO();
        userRO.setUserCode(userAccessRO.getUserCode());
        userRO.setUserAccess(userAccessRO);
        //实名信息
        Response<CustomerRO> customerResponse = userQueryService.queryUserByCustomerCode(userAccessRO.getCustomerCode());
        if(customerResponse.isSuccess()){
            CustomerRO customerRO = customerResponse.getData();
            if(null != customerRO){
                userRO.setCustomer(customerRO);
            }
        }
        //冷冻信息
        Response<List<UserFrozenRO>> frozensResponse = userFrozenApplication.load(userAccessRO.getUserCode());
        if(frozensResponse.isSuccess()){
            List<UserFrozenRO> frozenROS = frozensResponse.getData();
            if(CollectionUtils.isNotEmpty(frozenROS)){
                userRO.setFrozens(frozenROS);
            }
        }
        return Response.ok(userRO);
    }
}
